<template>
  <div class="user-management-container">
    <div class="header">
      <h1>用户管理</h1>
      <div class="button-group">
        <el-button type="primary" @click="showAddDialog">添加用户</el-button>
        <el-button type="warning" @click="showEditDialog" :disabled="!selectedUser">修改</el-button>
        <el-button type="danger" @click="handleDelete" :disabled="!selectedUser">删除</el-button>
        <el-button type="success" @click="downloadTemplate">下载模板</el-button>
        <el-upload
          class="upload-demo"
          action="/api/user/import"
          :on-success="handleImportSuccess"
          :on-error="handleImportError"
          :show-file-list="false"
        >
          <el-button type="success">导入Excel</el-button>
        </el-upload>
      </div>
    </div>
    <div class="content">
      <h2>用户列表</h2>
      <el-table 
        :data="tableData" 
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column prop="name" label="姓名" width="120" />
        <el-table-column prop="email" label="邮箱" width="180" />
        <el-table-column prop="phone" label="电话" width="150" />
        <el-table-column prop="address" label="地址" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
      </el-table>
    </div>

    <!-- 添加用户弹窗 -->
    <el-dialog
      v-model="dialogVisible"
      :title="isEdit ? '修改用户' : '添加用户'"
      width="500px"
    >
      <el-form :model="form" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" />
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone" />
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const tableData = ref([])
const dialogVisible = ref(false)
const selectedUser = ref(null)
const isEdit = ref(false)
const form = ref({
  id: null,
  name: '',
  email: '',
  phone: '',
  address: ''
})

// 获取用户列表
const fetchUsers = async () => {
  try {
    const response = await axios.get('/api/user/list')
    tableData.value = response.data
  } catch (error) {
    ElMessage.error('获取用户列表失败')
  }
}

// 显示添加用户弹窗
const showAddDialog = () => {
  isEdit.value = false
  form.value = {
    id: null,
    name: '',
    email: '',
    phone: '',
    address: ''
  }
  dialogVisible.value = true
}

// 显示修改用户弹窗
const showEditDialog = () => {
  if (!selectedUser.value) return
  isEdit.value = true
  form.value = {
    id: selectedUser.value.id,
    name: selectedUser.value.name,
    email: selectedUser.value.email,
    phone: selectedUser.value.phone,
    address: selectedUser.value.address
  }
  dialogVisible.value = true
}

// 提交表单（添加或修改）
const handleSubmit = async () => {
  try {
    if (isEdit.value) {
      await axios.put(`/api/user/update/${form.value.id}`, form.value)
      ElMessage.success('修改用户成功')
    } else {
      await axios.post('/api/user/add', form.value)
      ElMessage.success('添加用户成功')
    }
    dialogVisible.value = false
    fetchUsers()
  } catch (error) {
    ElMessage.error(isEdit.value ? '修改用户失败' : '添加用户失败')
  }
}

// 删除用户
const handleDelete = async () => {
  if (!selectedUser.value) return
  try {
    await axios.delete(`/api/user/delete/${selectedUser.value.id}`)
    ElMessage.success('删除用户成功')
    fetchUsers()
  } catch (error) {
    ElMessage.error('删除用户失败')
  }
}

// 处理表格选择变化
const handleSelectionChange = (selection) => {
  selectedUser.value = selection[0]
}

// 处理Excel导入成功
const handleImportSuccess = () => {
  ElMessage.success('导入成功')
  fetchUsers()
}

// 处理Excel导入失败
const handleImportError = () => {
  ElMessage.error('导入失败')
}

// 下载模板
const downloadTemplate = () => {
  window.open('/api/user/template', '_blank')
}

onMounted(() => {
  fetchUsers()
})
</script>

<style scoped>
.user-management-container {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.button-group {
  display: flex;
  gap: 10px;
}

h1 {
  font-size: 24px;
  color: #303133;
  margin: 0;
}

.content {
  background-color: white;
  padding: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}

h2 {
  font-size: 20px;
  color: #606266;
  margin-bottom: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 